<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class DiscloseController extends AdminController {
                                
    public function actionIndex() {

        $Breaking = new BreakingNews();
        $Breaking->unsetAttributes();

        if (isset($_GET['BreakingNews'])) {
            $Breaking->attributes = $_GET['BreakingNews'];
        }


        if (Yii::app()->request->isAjaxRequest) {
            if (isset($_POST['id']) && isset($_POST['status'])) {

                if ($_POST['status'] == 2) {
                    $sql_a = "update xyh_breaking_news set status=1 where id={$_POST['id']}";
                    $res = Yii::app()->db->createCommand($sql_a)->execute();
                }
            }
        }

        $this->render('index', array(
            'Breaking' => $Breaking
        ));
    }

    public function actionAdd() {

        $News = new News();
        $News->unsetAttributes();


        if ((isset($_POST['ajax']) && $_POST['ajax'] === 'news-form')) {
            //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($News));
            Yii::app()->end();
        }

        if (isset($_POST['News']) && $_POST['News']) {

            $News->alumni_id = $_SESSION['alumni_id'];
            $News->ctime = time();
            $News->attributes = $_POST['News'];

            if ($News->save()) {

                Yii::app()->user->setFlash('addform', "新闻添加成功!");
                $this->refresh();
            } else {

                Yii::app()->user->setFlash('addform', "新闻添加失败!");
                var_dump($News->getErrors());
                exit();
                barek;
            }
        }

        $this->render('add', array('News' => $News));
    }

    public function actionView($id) {

        $sql = "SELECT b.id,b.content,b.img_url,b.ctime,u.username,u.mobile,a.name
               FROM xyh_breaking_news as b
               LEFT JOIN {{user}} as u on b.user_id = u.id
               LEFT JOIN {{area}} as a on b.city_id = a.id
               WHERE b.id = {$id} ";


        $BreakingData = Yii::app()->db->createCommand($sql)->queryRow();

        //p_e($BreakingData);

        if ($BreakingData) {
            $this->renderPartial('view', array('Breaking' => $BreakingData), FALSE, TRUE);
        } else {
            throw new CHttpException(404, "无此爆料！");
        }
    }

    public function actionEdit($id) {

        $News = News::model()->findByPk($id);


        if ((isset($_POST['ajax']) && $_POST['ajax'] === 'news-form')) {
            //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($News));
            Yii::app()->end();
        }

        if (isset($_POST['News']) && $_POST['News']) {

            $News->alumni_id = $_SESSION['alumni_id'];
            $News->mtime = time();
            $News->attributes = $_POST['News'];

            if ($News->save()) {

                Yii::app()->user->setFlash('addform', "新闻修改成功!");
                $this->refresh();
            } else {


                Yii::app()->user->setFlash('addform', "新闻修改失败!");
                var_dump($News->getErrors());
                exit();
                barek;
            }
        }


        if ($News) {
            $this->render('edit', array('News' => $News));
        } else {
            throw new CHttpException(404, "无此新闻！");
        }
    }

    public function actionUeditor() {

        $new = new News();
        $this->render('ueditor', array('model' => $new));
    }

   
    
    
    public function showContent($data,$row,$c) {

       $showname = $data->content?'预览':'无内容';

     
        echo CHtml::link($showname, array('disclose/view','id' => $this->id),array('data-toggle' => 'modal', 'data-target' => '#view'));
       
       
    }
    
    
    
     public function actionDelete($id)
    {
        if(Yii::app()->request->isAjaxRequest){
            if($id){
                $breaking = BreakingNews::model()->findByPk($id);
                $breaking->is_del=1;
                $breaking->status=2;
               // $StoreData->name=$StoreData->name.'-'.date('Y-m-d H:i:s',time());
                if($breaking->save(FALSE))
                {
                    echo '删除成功';
                }else{
                   echo FALSE;
                   
                }
            }
            
        }
    }
     
     

}
